﻿using System;
using biz.ritter.javapi.util;
using Nofs.Fuse;
using Nofs.Net.Common.Interfaces.Domain;

namespace Nofs.Net.Domain.Impl
{
    public class FileObjectStat : BaseDomainObject, IFileObjectStat
    {

        private UUID _parentID;
        private String _name;
        private int _mode;
        private int _nlink;
        private int _uid;
        private int _gid;
        private int _rdev;
        private long _fileSize;
        private long _blocks;
        private int _atime;
        private int _mtime;
        private int _ctime;

        public FileObjectStat(UUID parentID, String name)
            : this(parentID, name, FuseFtypeConstants.TYPE_FILE | 0755, 0, 0, 0, 0, 0, 0, 0, 0, 0)
        {

        }

        public FileObjectStat(UUID parentID, String name, int mode, int nlink, int uid, int gid, int rdev, long fileSize, long blocks, int atime, int mtime, int ctime)
        {
            _name = name;
            _parentID = parentID;
            _mode = mode;
            _nlink = nlink;
            _uid = uid;
            _gid = gid;
            _rdev = rdev;
            _fileSize = fileSize;
            _blocks = blocks;
            _atime = atime;
            _mtime = mtime;
            _ctime = ctime;
        }

        public IFileObjectStat Clone() //throws Exception 
        {
            FileObjectStat stat = new FileObjectStat(_parentID, _name, _mode, _nlink, _uid, _gid, _rdev, _fileSize, _blocks, _atime, _mtime, _ctime);
            stat.SetID(GetID());
            if (IsNew())
            {
                stat.SetIsNew();
            }
            else
            {
                stat.SetIsNotNew();
            }
            return stat;
        }

        //@Override
        public String ParentName()
        {
            return _name;
        }

        //@Override
        public void SetParentName(String parentName)
        {
            _name = parentName;
        }

        //@Override
        public UUID ParentID()
        {
            return _parentID;
        }

        //@Override
        public bool Equals(IFileObjectStat stat)
        {
            return GetID().equals(stat.GetID());
        }

        //@Override
        public int getATime()
        {
            return _atime;
        }

        //@Override
        public long getBlocks()
        {
            return _blocks;
        }

        //@Override
        public int getCTime()
        {
            return _ctime;
        }

        //@Override
        public long getFileSize()
        {
            return _fileSize;
        }

        //@Override
        public int getGID()
        {
            return _gid;
        }

        //@Override
        public int getMTime()
        {
            return _mtime;
        }

        //@Override
        public int getMode()
        {
            return _mode;
        }

        //@Override
        public int getNLink()
        {
            return _nlink;
        }

        //@Override
        public int getRDev()
        {
            return _rdev;
        }

        //@Override
        public int getUID()
        {
            return _uid;
        }

        //@Override
        public void setATime(int value)
        {
            _atime = value;
        }

        //@Override
        public void setBlocks(long value)
        {
            _blocks = value;
        }

        //@Override
        public void setCTime(int value)
        {
            _ctime = value;
        }

        //@Override
        public void setFileSize(long value)
        {
            _fileSize = value;
        }

        //@Override
        public void setGID(int value)
        {
            _gid = value;
        }

        //@Override
        public void setMTime(int value)
        {
            _mtime = value;
        }

        //@Override
        public void setMode(int value)
        {
            _mode = value;
        }

        //@Override
        public void setNLink(int value)
        {
            _nlink = value;
        }

        //@Override
        public void setRDev(int value)
        {
            _rdev = value;
        }

        //@Override
        public void setUID(int value)
        {
            _uid = value;
        }

        private LinkedList<IExtendedAttribute> _xattr = new LinkedList<IExtendedAttribute>();

        public void AddXAttr(IExtendedAttribute attr)
        {
            _xattr.add(attr);
        }

        public LinkedList<IExtendedAttribute> GetAllXAttr()
        {
            return _xattr;
        }

        public IExtendedAttribute GetXAttr(String name) //throws Exception 
        {
            foreach (IExtendedAttribute attr in _xattr)
            {
                if (attr.getName().compareTo(name) == 0)
                {
                    return attr;
                }
            }
            return null;
        }

        public bool RemoveXAttr(IExtendedAttribute attr) //throws Exception 
        {
            if (!_xattr.remove(attr))
            {
                bool success = false;
                foreach (IExtendedAttribute actual in _xattr)
                {
                    if (actual.GetID().compareTo(attr.GetID()) == 0)
                    {
                        success = _xattr.remove(actual);
                        break;
                    }
                }
                return success;
            }
            return false;
        }
    }

}
